package com.dc.docs._888_面试.concurrent._7_threadpool;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Description: 线程池使用
 * @author: gdc
 * @date: 2021/6/17
 * @version 1.0
 */
public class ThreadPoolDemo {

    public static void main(String[] args){
        // 一次创建5个处理线程
        //ExecutorService executorService = Executors.newFixedThreadPool(5);
        //ExecutorService executorService = Executors.newSingleThreadExecutor();
        //ExecutorService executorService = Executors.newCachedThreadPool();

        // 真实项目中使用
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(2, 3,
                1L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());
        try {
            // 模拟10个用户来办理业务
            for (int i = 0; i < 10; i++) {
                executorService.execute(() -> System.out.println(Thread.currentThread().getName() + ", 办理业务"));
            }
        } finally {
            executorService.shutdown();
        }
    }

}
